import random
user_base=[
    {"账号":"30358655","姓名":"刘三","密码":123456,"地址":"中国北京市买唱片街道206","存款余额":2316,"开户行":"中国工商银行昌平支行"},
    {"账号":"65558585","姓名":"张四","密码":123456,"地址":"中国北京市买唱片街道206","存款余额":5006,"开户行":"中国工商银行昌平支行"},
    {"账号":"49865884","姓名":"王五","密码":123456,"地址":"中国北京市买唱片街道206","存款余额":8916,"开户行":"中国工商银行昌平支行"},
]
def save(account,savemoney):
    global user_base
    fh = 0
    ff=True
    str01 = str(account)
    for x in user_base:
        if x["账号"] == str01:
            x["存款余额"]+=savemoney
            fh = 2
            break
        else:
            continue
    if fh == 2:
        ff=True
    else:
        ff=False
    return ff
def draw(account,password,drawmoney):
    global user_base
    fh = 1
    str01 = str(account)
    for x in user_base:
        if x["账号"] == str01:
            fh=2
            if x["密码"]==password:
                if x["存款余额"]>=drawmoney:
                    x["存款余额"]-=drawmoney
                    fh =0
                    break
                else:
                    fh=3
                    break
            break
        else:
            continue

    return fh
def transfermoney(outaccount,inaccount,outpassword,outmoney):
    global user_base
    kk=1
    op=0
    str01 = str(outaccount)
    str02 = str(inaccount)
    for x66 in user_base:
        if x66["账号"] == str02:
            op=1
            break
        else:
            continue
    if op==1:
        for x in user_base:
            if x["账号"] == str01:
                if x["密码"]==outpassword:
                    if x["存款余额"] >= outmoney:
                        x["存款余额"] -= outmoney
                        kk = 0
                        break
                    else:
                        kk = 3
                        break
                else:
                    kk = 2
                    break
            else:
                continue
    if kk==0:
        for x66 in user_base:
            if x66["账号"] == str02:
                x66["存款余额"]+=outmoney
                break
            else:
                continue
    return kk
def query():
    print("账号        姓名   密码      地址                   存款余额   开户行")
    for xxww in user_base:
        info="%s   %s   %s   %s    %s     %s   "
        print(info % (xxww["账号"],xxww["姓名"],xxww["密码"],xxww["地址"],xxww["存款余额"],xxww["开户行"]))
def adduser(zh,xm,mm,dz,ckye,khh):
    dict01={}
    fh=0
    global user_base
    if len(user_base)>=100:
        fh=3
    else:
        for x in user_base:
            if x["账号"]==zh:
                fh=2
                break
            else:
                continue
        if fh==2:
            print("用户已存在")
        else:
            dict01["账号"]=zh
            dict01["姓名"]=xm
            dict01["密码"]=mm
            dict01["地址"]=dz
            dict01["存款余额"]=ckye
            dict01["开户行"]=khh
            user_base.append(dict01)
            print("添加成功")
            fh=1
    return fh
def str(st):
    '''
    字符串拼接
    :param st: 传入参数
    :return: 返回值
    '''
    str = ""
    for x in st:
        str += x
    return str
def user():
    st=[]
    for x_01 in range(10):
        a_01=str(x_01)
        st.append(a_01)
    account01=random.sample(st,8) # sample(序列，n) 从序列中随机取n个数以list消失返回 sample样本
    account=str(account01)
    print("账号已经生成：",*account)
    name=input("输入用户名：")
    while True:
        p=input("输入密码：")
        if p.isdigit() and len(p)==6:
            password=int(p)
            break
        else:
            print("输入错误，请输入6位数字！")
            continue
    country=input("请输入国家：")
    province=input("请输入省份：")
    street=input("请输入街道：")
    doorplate=input("请输入门牌号:")
    address=country+province+street+doorplate
    deposit=int(input("请输入存款余额："))
    bankstart="中国工商银行"
    adduser(account,name,password,address,deposit,bankstart)
def menu():
    while True:
        print("-------------中国工商银行--------------")
        print("-------------账户管理系统--------------")
        print("--------------  V1.0   --------------")
        print("-------------------------------------")
        print("1.开户                                ")
        print("2.存钱                                ")
        print("3.取钱                                ")
        print("4.转账                                ")
        print("5.查询                                ")
        print("6.退出                                ")
        ia=int(input("请输入数字[1-6]:"))
        if ia==1:
            user()
        elif ia==2:
            ck=save(input("请输入账号："),int(input("请输入存款金额：")))
            if ck:
                print("存款成功")
            else:
                print("没有该用户")
        elif ia==3:
            qk = draw(input("请输入账号："),int(input("请输入密码：")), int(input("请输入取款金额：")))
            if qk==0:
                print("正常取款成功")
            if qk==1:
                print("账号不存在")
            if qk==2:
                print("密码错误")
            if qk==3:
                print("钱不够")
        elif ia==4:
            zz = transfermoney(input("请输入转出账号："),input("请输入转入账号："), int(input("请输入转出账号密码：")), int(input("请输入转账金额：")))
            if zz == 0:
                print("正常转账成功")
            if zz == 1:
                print("账号不对")
            if zz == 2:
                print("密码错误")
            if zz == 3:
                print("钱不够")
        elif ia==5:
            query()
            input("按enter继续")

        elif ia==6:
            break
        else:
            print("输入错误，请重新输入！")
menu()
